package ai.engine;

import ai.Trackable;
import ai.PathFinding;
import ai.AIUtil;
import engine.GameEngine;
import engine.UnitAction;
import engine.data.Player;
import engine.data.Unit;
import engine.data.UnitType;

public class AIUnit extends Unit implements TaskDoer {

	public Task task;

	public byte lastDirection = UnitAction.NONE;

	public AIUnit(Player unitPlayer, byte unitType) {
		super(unitPlayer, unitType);
	}

	public short getX() {
		return x;
	}

	public short getY() {
		return y;
	}

	public short getETA() {
		if (task==null) return -1;
		short eta;
		Trackable target = (Trackable) task.target;
		if (target != null) {
			eta = (short) (Math.abs(PathFinding.getHeuristic(this.getX(), this.getY(), target.getX(), target.getY())));
		} else {
			eta = (short) ((100 - AIUtil.getCurrentAIPlayer().explorationPourcentage) / 100 * PathFinding.getHeuristic((short) 0, (short) 0, (short) (GameEngine.map.width - 1), (short) (GameEngine.map.height - 1)));
		}
		return eta;
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public void resetTask() {
		task = null;
	}

	public final String toString() {
		short[] lastSeenPosition = this.getLastSeenPosition(GameEngine.currPlayer);
		return new StringBuffer("AIUnit[")
			.append(this.getId())
			.append(" ")
			.append(UnitType.getName(this.type))
			.append(" real(")
			.append(this.x)
			.append(" , ")
			.append(this.y)
			.append(") ")
			.append(" lastSeen(")
			.append(lastSeenPosition[0])
			.append(" , ")
			.append(lastSeenPosition[1])
			.append(") ")
			.append(" Civ ")
			.append(this.player.civilization)
			.append(" ]")
			.toString();
	}
}
